﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using SysFrame.BP;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MyAppBox
{
    public static class SysFrameServiceExtensions
    {
        public static IServiceCollection AddSysFrame(this IServiceCollection services, IConfiguration config)
        {
            DbHelperMySQL.SetConnectionString(GetConnectionString(config));
            return services;
        }


        /// <summary>
        /// 得到数据路径
        /// </summary>
        /// <returns></returns>
        private static string GetConnectionString(IConfiguration Configuration)
        {
            string SQLTYPE = Configuration["DataConfig:SQLTYPE"];
            string server = Configuration["DataConfig:SQLServer:SERVER"];
            string database = Configuration["DataConfig:SQLServer:DATABASE"];
            string uid = Configuration["DataConfig:SQLServer:USER"];
            string pwd = Configuration["DataConfig:SQLServer:PASSWORD"];
            string config = "server=" + server + ";database=" + database + ";uid=" + uid + ";pwd=" + pwd + "";
            if (SQLTYPE == "2")
            {
                server = Configuration["DataConfig:MySQL:SERVER"];
                database = Configuration["DataConfig:MySQL:DATABASE"];
                uid = Configuration["DataConfig:MySQL:USER"];
                pwd = Configuration["DataConfig:MySQL:PASSWORD"];
                config = "server=" + server + ";database=" + database + ";user id=" + uid + ";password=" + pwd + ";Charset=gbk";
            }
            DataBase.SQLTYPE = SQLTYPE;
            return config;
        }
    }
}
